package org.csu.demo.controller;

import org.csu.demo.domain.Account;
import org.csu.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

//括号里是写在form表单action里的,客户端来访问的url
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    //返回页面为thymeleaf，只需返回的名字，类型是String，不用加后缀
    @PostMapping("/login")//http://localhost:8080/account/login
    public String login(Account account, Model model){
        //用于测试数据是否传到
        System.out.println(account.getUsername()+","+account.getPassword());
        //loginAccount表示已经登陆的账户，若成功登陆为account，不成功为空。
        Account loginAccount = accountService.login(account);
        if(loginAccount==null){
            //通过model模型，前端可以通过msg获取字段
            model.addAttribute("msg", "用户名或密码错误，请重新输入!");
            return "index";
        }else {
            model.addAttribute("loginAccount", loginAccount);
            return "success";
        }



    }

}
